import Mock from 'mockjs'
import {NEW, POP, SELL} from "@/common/const";
import images from '@/mock/common/images'
import { getQuery } from "@/mock/common/util";

const data = Mock.mock({
  [NEW]:{
    "list|25": [
      {
        "id": '@increment(1)',
        "type": NEW,
        "img|+1": images.images,
        "title": "@ctitle(5,20)",
        "price|10-100.2": 0,
        "cfav|10-666": 0
      }
    ]
  },
  [POP]:{
    "list|12": [
      {
        "id": '@increment(1)',
        "type": POP,
        "img|+1": images.images,
        "title": "@ctitle(5,20)",
        "price|10-100.2": 0,
        "cfav|10-666": 0
      }
    ]
  },
  [SELL]:{
    "list|7": [
      {
        "id": '@increment(1)',
        "type": SELL,
        "img|+1": images.images,
        "title": "@ctitle(5,20)",
        "price|10-100.2": 0,
        "cfav|10-666": 0
      }
    ]
  },
})

// console.log(data);

export default {
  'get|/home/data': options => {
    // 获取传递的参数type
    const type = getQuery(options.url,'type')
    // 获取传递的参数pageindex
    const pagenum = getQuery(options.url,'page')
    // 获取传递的参数pagesize
    const pagesize = getQuery(options.url,'pagesize')
    // 截取数据的起始位置
    const start = (pagenum-1)*pagesize
    // 截取数据的终点位置
    const end = pagenum*pagesize
    // 计算总页数
    let targetList = data[type].list
    
    const totalPage = Math.ceil(targetList.length/pagesize)
    // console.log(data, type, targetList, totalPage);
    // 数据的起始位置：(pageindex-1)*pagesize  数据的结束位置：pageindex*pagesize
    const list = pagenum>totalPage?[]:targetList.slice(start,end)
    return {
      status: 200,
      message: '获取列表成功',
      list: list,
      total: targetList.length
    }
  },
}